package com.ty.spring.config;

import com.ty.job.MyJob;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.UUID;

/**
 * Quartz配置类
 *
 * @Author Tommy
 * @Date 2023/10/22
 */
@Configuration
public class QuartzConfig {

    @Bean
    public JobDetail myJobDetail() {
        return JobBuilder.newJob(MyJob.class)
                .withIdentity("myJob")
                .withDescription("我的Quartz Job")
                .storeDurably()
                .usingJobData("author", "Tommy")
                .usingJobData("retry", 0)
                .build();
    }

    //@Bean
    public Trigger myJobTrigger() {
        Date start = Date.from(LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant());
        System.out.print("触发器开始时间：");
        System.out.println(start);

        Integer any = null;
        return TriggerBuilder.newTrigger()
                .withIdentity("myJobTrigger")
                //.forJob(myJobDetail())
                .forJob("myJob")
                .usingJobData("singal", 1)
                .usingJobData("deviceId", UUID.randomUUID().toString())
                .usingJobData("指令Charge", "Charge11")
                .usingJobData("any", any)
                .startAt(start)
                .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
                .build();
    }
}
